/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaSE;

import java.sql.*;

/**
 *
 * @author Rui
 */
public class DBConnection {
    private Connection dbc=null;
    public DBConnection(){
        this.dbc=getConnection();
    }

    protected Connection getConnection(){
        Connection conn = null;
        try{
           Class.forName("com.mysql.jdbc.Driver").newInstance();
           conn = DriverManager.getConnection("jdbc:mysql://localhost/feedanalyzer?user=root&password=");
        }
        catch (Exception ex) {
        System.out.println("Failed to get connection: " + ex.getMessage());
        }
        return conn;
    }
    public ResultSet executeQuery(String query)
	throws SQLException {
	Statement stmt = null;

	ResultSet rs = null;


	// Create a Statement
	stmt = dbc.createStatement();
    System.out.println(query);
	// Query
	rs = stmt.executeQuery(query);
	if(rs==null)
      System.out.println("ERROR");
	return rs;


     }
    public void executeUpdate(String query)
	throws SQLException {
	Statement stmt = null;

	// Create a Statement
	stmt = dbc.createStatement();

	// Query
	stmt.executeUpdate(query);

	stmt.close();
}

/* public static void main(String [ ] args) throws SQLException{
        DBConnection DBConn = new DBConnection();
        String UserName="123241";
         String sqlStatement="select * from users where UserName='"+UserName+"'";
        ResultSet   result = DBConn.executeQuery(sqlStatement);
       while(result.next())
       {
          System.out.print( result.getString("UserName"));

       }
  }*/



}
